import smtplib

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText


class TestEmailReport:

    def test_send_report_by_email(self):
        """
        发送报告到邮箱：
        1、创建邮箱对象，填充信息（发、收邮箱，主题，附件）
        2、发送邮箱
        :return:
        """
        # SMTP服务器地址
        smtp_server = "smtp.qq.com"
        # 发送邮箱的账号和密码
        user_name = "linza.xie@foxmail.com"
        auth_code = "ufyknglhdfgidfha"  # 授权码
        # 接收的邮箱
        receive_email = "hylandz@foxmail.com"
        message = MIMEMultipart('related')  # 邮件对象
        subject = 'XXXX-项目测试报告'  # 主题
        attach_file = MIMEText(open('report.html', 'rb').read(), 'html', 'utf-8')
        # 填充邮件对象（发、收邮箱，主题，附件）
        message['from'] = user_name
        message['to'] = receive_email
        message['subject'] = subject
        message.attach(attach_file)
        # 登录smtp并发送邮箱
        smtp = smtplib.SMTP()
        smtp.connect(smtp_server)
        smtp.login(user_name, auth_code)
        smtp.sendmail(user_name, receive_email, message.as_string())
        smtp.quit()


if __name__ == '__main__':
    TestEmailReport().test_send_report_by_email()
